package com.eryos.android.holidaybudget;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.eryos.android.holidaybudget.beans.Budget;
import com.eryos.android.holidaybudget.beans.Expense;

/**
 * Project :  Holiday'sBudget
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/Holiday'sBudget/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class ExpenseActivity extends RootActivity {
	
	EditText budgetField;
	EditText paymentField;
	EditText categoryField;
	EditText amountField;
	EditText commentField;
	Button 	 validButton;
	
	@Override
    protected void initParam(){
		Log.d(LOG_TAG, "initParam");

		viewTitle = "Expense";
		hasTopBanner = true;
	}

    @Override
    protected void initUI(){
        Log.d(LOG_TAG, "initUI");
 
        /* Define the Layout */
        setContentView(R.layout.expense);
        
        showWaitingMessage();
    	
        /* Retrieve UI elements */
    	budgetField 	= (EditText) findViewById(R.id.expense_budget_field);
    	paymentField	= (EditText) findViewById(R.id.expense_payment_field);
    	categoryField 	= (EditText) findViewById(R.id.expense_category_field);
    	amountField 	= (EditText) findViewById(R.id.expense_amout_field);
    	commentField 	= (EditText) findViewById(R.id.expense_comment_field);
    	validButton 	= (Button) 	 findViewById(R.id.expense_create_button); 


    }
    
    @Override
    protected void initHandlers(){
    	super.initHandlers();
    	
    	Log.d(LOG_TAG, "initHandlers");    	

    	validButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Log.d(LOG_TAG, "Click Valid Button");
            	
            	/* Retrieve value from UI components */
                Expense expense = new Expense();
                expense.setBudgetName(budgetField.getText().toString());
                expense.setCategory(categoryField.getText().toString());
                expense.setPayment(paymentField.getText().toString());
                expense.setComment(commentField.getText().toString());
                Float amount = new Float(amountField.getText().toString());
                expense.setAmount(amount);
  
                /* Store expense in DB */
                new BackgroundTask().execute(expense);
            }
        });
    	
    }

    @Override
    protected void fetchData(){
        Log.d(LOG_TAG, "fetchData");

        refreshUI();
    }

    @Override
	protected void refreshUI() {
    	Log.d(LOG_TAG, "refreshUI");

    	hideWaitingMessage();
	}

    /**
     * 
     * String : arguments type
     * Integer : progress value
     * Bitmap : return type
     */
    private class BackgroundTask extends AsyncTask<Expense, Integer, Boolean> { 

		/** 
		 * Step 1 - invoked on the UI thread immediately after the task is executed. 
		 * This step is normally used to setup the task, for instance by showing a progress bar in the user interface 
		 */
		protected void onPreExecute() { 
	        Log.d(LOG_TAG, "BG 1/3 - onPreExecute");
		} 

		/** 
		 * Step 2 - invoked on the background thread immediately after onPreExecute() finishes executing. 
		 * This step is used to perform background computation that can take a long time 
		 */
		protected Boolean doInBackground(Expense... args) { 
	        Log.d(LOG_TAG, "BG 2/3 - doInBackground");

	        for ( Expense expense : args){
	        	System.out.println(expense);
	        	db.insertExpense(expense);
			}

			return true;
		} 

		/** 
		 * Step 3 - invoked on the UI thread after the background computation finishes. 
		 * The result of the background computation is passed to this step as a parameter. 
		 */
		protected void onPostExecute(Boolean  result) {
	        Log.d(LOG_TAG, "BG 3/3 - onPostExecute");

	        refreshUI();
		}
		
		 protected void onProgressUpdate(Integer... progress) {
		        Log.d(LOG_TAG, "BG - onProgressUpdate");
	         //setProgressPercent(progress[0]);
	     }
	}

}
